package com.example.interview.no0106.solution;

import com.example.interview.no0106.Solution;

/**
 * @Description 解答-字符串压缩
 * @ClassName ArraySolution
 * @Author Administrator
 * @Date 2020/7/7 19:36
 * @Version 1.0.0
 */
public class WhileSolution implements Solution {
    @Override
    public String compressString(String s) {
        int len = s.length();
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (i < len) {
            int j = i;
            while (j < len && s.charAt(j) == s.charAt(i)) {
                j++;
            }
            sb.append(s.charAt(i));
            sb.append(j - i);
            i = j;
        }

        String res = sb.toString();
        if (res.length() < s.length()) {
            return res;
        } else {
            return s;
        }
    }
}
